package com.ning.weixin.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.ning.weixin.entity.AcWechatInfo;

/**
 * @author MENG_LI_DONG
 * @version 2018年12月18日 下午10:13:45
 */
@Component
public class AcWechatInfoDao {
	private Logger log = LoggerFactory.getLogger(AcWechatInfoDao.class);

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public void insert(AcWechatInfo t) {
		final StringBuffer sql = new StringBuffer();
		StringBuffer sqlParams = new StringBuffer();
		final List<Object> params = new ArrayList<>();
		sql.append("insert into ac_wechat_info(create_time,update_time,");
		sqlParams.append("?,?,");
		params.add(new Date());
		params.add(params.get(0));
		if (t.getUserId() != null) {
			sql.append("user_id,");
			sqlParams.append("?,");
			params.add(t.getUserId());
		}
		if (t.getOpenid() != null) {
			sql.append("openid,");
			sqlParams.append("?,");
			params.add(t.getOpenid());
		}
		if (t.getNickname() != null) {
			sql.append("nickname,");
			sqlParams.append("?,");
			params.add(t.getNickname());
		}
		if (t.getSex() != null) {
			sql.append("sex,");
			sqlParams.append("?,");
			params.add(t.getSex());
		}
		if (t.getProvince() != null) {
			sql.append("province,");
			sqlParams.append("?,");
			params.add(t.getProvince());
		}
		if (t.getCity() != null) {
			sql.append("city,");
			sqlParams.append("?,");
			params.add(t.getCity());
		}
		if (t.getCountry() != null) {
			sql.append("country,");
			sqlParams.append("?,");
			params.add(t.getCountry());
		}
		if (t.getHeadimgurl() != null) {
			sql.append("headimgurl,");
			sqlParams.append("?,");
			params.add(t.getHeadimgurl());
		}
		sql.deleteCharAt(sql.length() - 1);
		sqlParams.deleteCharAt(sqlParams.length() - 1);
		sql.append(") values(").append(sqlParams).append(")");
		log.debug("sql:" + sql);
		try {
			jdbcTemplate.update(sql.toString(), params.toArray());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}
	public void update(AcWechatInfo t){}
	public AcWechatInfo getByOpenid(String openid){
		String sql = "select t.* from ac_wechat_info t where t.openid = ?";
		try {
			return jdbcTemplate.queryForObject(sql, new String[]{openid}, new RowMapper<AcWechatInfo>(){
				@Override
				public AcWechatInfo mapRow(ResultSet rs, int row)
						throws SQLException {
					AcWechatInfo t = new AcWechatInfo();
					Timestamp createTime = rs.getTimestamp("create_time");
					if(createTime!=null){
						t.setCreateTime(new Date(createTime.getTime()));
					}
					t.setCity(rs.getString("city"));
					t.setCountry(rs.getString("country"));
					t.setHeadimgurl(rs.getString("headimgurl"));
					t.setId(rs.getLong("id"));
					t.setNickname(rs.getString("nickname"));
					t.setOpenid(rs.getString("openid"));
					t.setProvince(rs.getString("province"));
					t.setSex(rs.getInt("sex"));
					t.setUserId(rs.getLong("user_id"));
					Timestamp updateTime = rs.getTimestamp("update_time");
					if(updateTime!=null){
						t.setUpdateTime(new Date(updateTime.getTime()));
					}
					return t;
				}
			});
		} catch(Exception e) {
			return null;
		}
	}
}
